<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@include file="/WEB-INF/ibnms/common/easyui.jsp" %>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>角色管理</title>
		<link href="<%=application.getContextPath()%>/nresources/ued/css/all-src.css" type="text/css" rel="stylesheet" />
		<%@include file="/WEB-INF/ibnms/common/jqgrid.jsp"%>
		<%@include file="/WEB-INF/ibnms/common/ztree.jsp" %>
</head>

<body>
<div class="form-wrapper">
    	<div class="form-title">
       		 <span class="ttcorner"></span>
             <h3>
                <span class="text">角色管理</span>
             </h3>
        </div>
        <div id="role-rel-module-form" class="block02" style="display:none">
			<div class="title-style02">
				<input id="submitBtn" type="button" class="btn-style02" align="middle" value="保存" />
				<input id='returnBtn' type='button' class="btn-style02" value='返回' />
			</div>
			<div style="height:80%">
				<ul id="moduleManageTree" class="ztree" style="width:100%; height:100%;overflow-y:auto;overflow-x:hidden"></ul>
			    <div id='loadingmoduleTree' style='display:none;'><img src="<%=application.getContextPath()%>/njs/basd/ztree/css/zTreeStyle/img/loading.gif"></img>load....</div>
			</div>
		</div>
        <div id="role-form" class="block02">
        	<div class="title-style02">
	        	<input class="btn-style02" type="button" id="btn-add" value="增加">&nbsp;&nbsp;
				<input class="btn-style02" type="button" id="btn-edit" value="修改">&nbsp;&nbsp;
				<input class="btn-style02" type="button" id="btn-del" value="删除">&nbsp;&nbsp;
				<input class="btn-style02" type="button" id="btn-moduleadd" value="模块关联">&nbsp;&nbsp;
			</div>
			<table id="gridTable" width="80%">
			</table>
			<div id="gridPager">
			</div>
			<form name="theForm" method="post" >
				<input type="hidden" name="roleForm.ROLE_ID" value="${roleForm.ROLE_ID}"/>
				<input type="hidden" name="roleForm.PARENT_ID" value="${roleForm.PARENT_ID}"></input>
			</form>
		</div>
</div>

</body>
		<script type="text/javascript">
			var $moduleTreeObj;
			var roleId;
			$(function(){
				$("#gridTable").jqGrid({
					url: "loadListRoleQueryJson.do",
					datatype: "json",
					mtype: "POST",
					height: "auto",
					autowidth: true,
					colNames:['角色ID', '角色名称', '角色描述', '是否有效'],
					colModel:[
						{name:'ROLE_ID',index:'ROLE_ID',hidden:true},
						{name:'ROLE_NAME',index:'ROLE_NAME'},
						{name:'ROLE_DESC',index:'ROLE_DESC'},
						{name:'GRANT_ENABLE',index:'GRANT_ENABLE',formatter: function(value){
							if (value=="1"){
								return "是";
							} else {
								return "否";
							}
						}}
					],
					viewrecords: true,
					rowNum: 15,
					rowList: [10,15,30],
					jsonReader: {
						root: 'rows',
						page: 'page',
						total: 'total',
						records: 'records',
						repeatitems: false,
						subgrid: {
					        root: 'rows',
							page: 'page',
							total: 'total',
							records: 'records',
							repeatitems: false
					    }      
					},
					pager: "#gridPager",
					hidegrid: true,
					multiselect: true,
					beforeRequest:function(){
						var postData = {};
						$(":input[name^='roleForm.']").each(function(){
							postData[$(this).attr("name")] = $(this).val();
						});
						$.extend($("#gridTable").getGridParam("postData"), postData);
					}
				});
				
				$moduleTreeObj = $.fn.zTree.init($("#moduleManageTree"), 
				{
					treeId:"id",
					data: {
						key:{
	                           name:'name',
	                           title:'name',
	                           id:'id'
	                       },
	                       simpleData:{
	                           idKey:'id'
	                       }				
					},
					check:{
						enable:true,
						autoCheckTrigger:true,
						chkboxType:{ "Y": "sp", "N": "sp" }
					},
					callback:{
						onNodeCreated:function(event, treeId, treeNode){
							if($(treeNode).attr("pId") == "0"){
								$.ajax({
									type:"post",
									url:"loadModuleInfoBySystemId.do?systemId="+$(treeNode).attr("id"),
									dataType:"json",
									success:function(msg){
										msg = eval(msg);
										var newNode = $moduleTreeObj.transformTozTreeNodes(msg);
										$moduleTreeObj.addNodes(treeNode,newNode,true);
									}
								});
							}
						}
					}
				},[]);
				
				$.ajax({
					type:"post",
					url:"loadSystemNodeForJson.do?systemId=0",
					dataType:"json",
					success:function(msg){
						msg = eval(msg);
						$moduleTreeObj.addNodes(null,msg,false);
					}
				});
				
				$("#submitBtn").click(function(){
					var checkNodes = $moduleTreeObj.getCheckedNodes(true);
					var cfgNodeIds = "";
					$.each(checkNodes,function(idx,node){
						cfgNodeIds += "#" + $(node).attr("id");
					})
					cfgNodeIds = cfgNodeIds.substring(1);
					$.ajax({
						type:"post",
						dataType:"json",
						url:"saveRoleModel.do",
						data:"theForm.roleID="+roleId+"&theForm.moduleIds="+cfgNodeIds,
						beforeSend:function(){
							$("#submitBtn").val("保存中....");
							$("#submitBtn").attr("disabled","disabled");
						},
						success:function(msg){
							if(msg == 'true'){
								$("#submitBtn").val("保存");
								$("#submitBtn").removeAttr("disabled");
							}
						}
					})
				});
				
				$("#returnBtn").click(function(){
					$("#role-rel-module-form").toggle("slow");
					$("#role-form").toggle("slow");
				});
				
			}); 
			
			
			$("#btn-search").click(function(){
				$("#gridTable").trigger("reloadGrid",[{page:1}]);
			});
			
			$("#btn-add").click(function(){
				$("form[name='theForm']").append("<input type='hidden' name='roleForm.ISMP_JSP_COMMAN' value='add'/>");
				$("form[name='theForm']").attr("action","<s:url action='editRole' namespace='/'/>").submit();
			});
			$("#btn-del").click(function(){
			 	var idArr = jQuery("#gridTable").jqGrid('getGridParam','selarrrow');
                if (idArr.length == 0) {
                    alert("请先选择要删除的记录!");
                    return;
                }
                
				if(confirm("是否删除所选的记录?")){
					var ROLE_ID = "";
                    for (var one in idArr){
                    	var data = jQuery("#gridTable").jqGrid('getRowData',idArr[one]);
                    	ROLE_ID += data.ROLE_ID + ",";
                    }
                    if(ROLE_ID.length>0){
                    	ROLE_ID = ROLE_ID.substring(0,ROLE_ID.length-1);
                    }
					$.ajax({
						type: "POST",
						async: false,
						url: "<s:url action="delRole" namespace='/'/>?roleForm.ROLE_ID="+ROLE_ID+"&roleForm.PARENT_ID="+$("input[name='roleForm.PARENT_ID']").val(),
						success: function(data){
	                        if (data != "true"){
								alert(data);
	                        }else{
	                       		$("#gridTable").trigger('reloadGrid');
	                       		alert("删除成功");
	                       		parent.refreshNode();
	                       	}
						},
						error:function(xhr){  
							show_error(xhr);
						}
					});	
                }
			});
			$("#btn-edit").click(function() {
				var idArr = jQuery("#gridTable").jqGrid('getGridParam','selarrrow');
				switch (idArr.length) {
					case 0:
						alert("请先选择要修改的记录!");
						break;
					case 1:
						var data = jQuery("#gridTable").jqGrid('getRowData',idArr[0]);
						var ROLE_ID = data.ROLE_ID;
						$("input[name='roleForm.ROLE_ID']").val(ROLE_ID);
						$("form[name='theForm']").append("<input type='hidden' name='roleForm.ISMP_JSP_COMMAN' value='modify'/>");
						$("form[name='theForm']").attr("action","<s:url action='editRole' namespace='/'/>");
						$("form[name='theForm']").submit();
						break;
					default:
						alert("一次只能修改一条记录!");
				}
			}); 
			
			$("#btn-moduleadd").click(function(){
				// 获取其中选择的信息
				var idArr = jQuery("#gridTable").jqGrid('getGridParam','selarrrow');
				if(idArr.length == 0){
					alert("请选择角色！");
					return;
				}
				if(idArr.length > 1){
					alert("您一次只能给一个角色设置模块！");
					return;
				}
				var data = jQuery("#gridTable").jqGrid('getRowData',idArr[0]);
				$("#role-rel-module-form").toggle("slow");
				$("#role-form").toggle("slow");
				roleId = data.ROLE_ID;
				setTimeout("loadCheckNode()",100);
			});
			
			function loadCheckNode(){
				$.ajax({
					type:"post",
					url:"findModuleIdByRoleId.do?roleForm.ROLE_ID="+roleId,
					dataType:"json",
					success:function(msg){
						msg = eval(msg);
						$moduleTreeObj.checkAllNodes(false);
						$moduleTreeObj.expandAll(false);
						var nodes = $moduleTreeObj.getNodes();
						/*判断哪些节点需要被选中*/
						for(var i=0;i<nodes.length;i++){
							eachChildren(nodes[i],msg);
						}
						$("#loadingmoduleTree").hide();
						$("#moduleManageTree").show();
					}
				});
			}
			
			function eachChildren(node,rel){
				if(node.isParent){
					var childNode = node.children;
					if(childNode != null && node.children.length>0){
						var b = true;
						for(var j = 0 ; j < childNode.length ; j ++){
							eachChildren(childNode[j],rel);
						}
					}else{
						for(var i = 0 ; i < rel.length; i++){
							if(node.id == rel[i]){
								$moduleTreeObj.checkNode(node, true, true);
							}
						}
					}
				}else{
					for(var i = 0 ; i < rel.length; i++){
						if(node.id == rel[i]){
							$moduleTreeObj.checkNode(node, true, true);
						}
					}
				}
			}
		</script>
</html>
